/**
* Copyright (C) 2018-2022
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.tanzer.service.impl;

import co.yixiang.modules.tanzer.domain.ManicuristProductRecord;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.modules.tanzer.domain.Product;
import co.yixiang.modules.tanzer.domain.ProductStockRecord;
import co.yixiang.modules.tanzer.service.ProductService;
import co.yixiang.modules.tanzer.service.mapper.ProductMapper;
import lombok.AllArgsConstructor;
import co.yixiang.dozer.service.IGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.tanzer.service.ManicuristProductRecordService;
import co.yixiang.modules.tanzer.service.dto.ManicuristProductRecordDto;
import co.yixiang.modules.tanzer.service.dto.ManicuristProductRecordQueryCriteria;
import co.yixiang.modules.tanzer.service.mapper.ManicuristProductRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import co.yixiang.domain.PageResult;
/**
* @author tanzer
* @date 2024-08-03
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "manicuristProductRecord")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ManicuristProductRecordServiceImpl extends BaseServiceImpl<ManicuristProductRecordMapper, ManicuristProductRecord> implements ManicuristProductRecordService {

    private final IGenerator generator;

    @Autowired
    private ManicuristProductRecordMapper manicuristProductRecordMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    //@Cacheable
    public PageResult<ManicuristProductRecord> queryAll(ManicuristProductRecord criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<ManicuristProductRecord> page = new PageInfo<>(manicuristProductRecordMapper.selectAllList(criteria));
        return generator.convertPageInfo(page,ManicuristProductRecord.class);
    }


    @Override
    //@Cacheable
    public List<ManicuristProductRecord> queryAll(ManicuristProductRecordQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(ManicuristProductRecord.class, criteria));
    }


    @Override
    public void download(List<ManicuristProductRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ManicuristProductRecordDto manicuristProductRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("美甲师id", manicuristProductRecord.getManicuristId());
            map.put("产品id", manicuristProductRecord.getProductId());
            map.put("xs数量", manicuristProductRecord.getNumXscm());
            map.put("s数量", manicuristProductRecord.getNumScm());
            map.put("m数量", manicuristProductRecord.getNumMcm());
            map.put("L数量", manicuristProductRecord.getNumLcm());
            map.put("总数量", manicuristProductRecord.getTotalNum());
            map.put("备注", manicuristProductRecord.getRemake());
            map.put("创建日期", manicuristProductRecord.getCreateTime());
            map.put(" updateTime",  manicuristProductRecord.getUpdateTime());
            map.put(" isDel",  manicuristProductRecord.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public boolean enterProduct(ManicuristProductRecord resources) {
        Product product = productMapper.selectById(resources.getProductId());
        Long xs = product.getNumXscm()+resources.getNumXscm();
        Long s = product.getNumScm()+resources.getNumScm();
        Long m = product.getNumMcm()+resources.getNumMcm();
        Long l = product.getNumLcm()+resources.getNumLcm();
        product.setNumXscm(xs);
        product.setNumScm(s);
        product.setNumMcm(m);
        product.setNumLcm(l);
        productMapper.updateById(product);
        ProductStockRecord productStockRecord = new ProductStockRecord();
        productStockRecord.setProductId(resources.getProductId());
        productStockRecord.setNumXscm(resources.getNumXscm());
        productStockRecord.setNumScm(resources.getNumScm());
        productStockRecord.setNumMcm(resources.getNumMcm());
        productStockRecord.setNumLcm(resources.getNumLcm());
        productStockRecord.setMinusOrAdd("add");
        productStockRecord.setNum(resources.getProductId()+resources.getNumScm()+resources.getNumMcm()+resources.getNumLcm());

        return false;
    }
}
